import axios from 'axios'
import { ElMessage } from 'element-plus'
import router from '../router'
//创建axios实力对象

const request = axios.create({
    baseURL:'/api',
    //远程请求超时时间
    timeout:6000000
})



//axios的请求 request 拦截器, 每次请求获取localStorage中的loginUser,
//  从中获取到token, 在请求头token中携带到服务端
request.interceptors.request.use(
    (config) => {
            let loginUser= JSON.parse(localStorage.getItem('loginUser'))
            console.log(localStorage.getItem('loginUser'))
            if (loginUser) {
            config.headers.token = loginUser.token
            }
            return config
    }
)



//axios 的相应response拦截器
request.interceptors.response.use(
    (response)=>{
        return response.data
    },
    (error) =>{
        //如果响应的状态码为401, 则路由到登录页面
        if(error.response.status===401){
            ElMessage.error('登录失效')
            router.push('/login')
        }
        return Promise.reject(error)
    }
)

export default request
